package com.xiaoyu.base.utils.extensions

import `in`.srain.cube.util.Env
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.model.User

// dimension
val Float.dp: Float
    get() {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                this,
                Resources.getSystem().displayMetrics
        )
    }

val Int.dp: Float
    get() {
        return this.toFloat().dp
    }

fun Throwable.printStackTraceForCp() {
    this.printStackTrace()
    if (Env.isDevOrInDevMode()) {
        GlobalUI.getInstance().showToast(this.localizedMessage)
    }
}

// context
fun Context.copy(text: String) {
    val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    cm.primaryClip = ClipData.newPlainText("", text)
}

// user
fun User.isSystemAccount(): Boolean {
    return UserData.getInstance().isSystemAccount(this)
}